package com.lagou.config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import java.util.Set;

/**
 * @author: jie.zhao
 * @create: 2021/03/16 22:38
 */
public class SpringServletContainer implements ServletContainerInitializer {


//    public void onStartup(ServletContext servletContext) throws ServletException {
//        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
//        ac.register(AppConfig.class);
//
//        DispatcherServlet ds = new DispatcherServlet();
//        ServletRegistration.Dynamic registration = servletContext.addServlet("/", ds);
//        registration.setLoadOnStartup(1);
//        registration.addMapping("/*");
//    }

    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
        ac.register(AppConfig.class);

        DispatcherServlet ds = new DispatcherServlet(ac);
        ServletRegistration.Dynamic registration = servletContext.addServlet("/", ds);
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");
    }
}
